<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'data'.DS.'DAOUserRoutes.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class JDAOUserRoutes implements DAOUserRoutes{
	
	public function __construct(){
		
	}
	
	private function deleteQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$db->query();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return true;
	}
	
	private function multiObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $rows;
	}	
	
	private function singleObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$row = $db->loadObject();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $row;
	}
	
	public function getUserRoutes($userId){
		$rows=$this->multiObjectQuery("SELECT * FROM #__sr_userroutes WHERE userid=".$userId);
		return DTOFactory::getInstance()->getDTOUserRoutes($rows);
	}
	
	public function addUserRoute($routeID,$userID){
		$row=$this->singleObjectQuery("SELECT id FROM #__sr_userroutes WHERE routeID=".$routeID." AND userID=".$userID);
		if(!$row){
			$row =& JTable::getInstance('userroutes', 'Table');
			$row->userID=$userID;
			$row->routeID=$routeID;
			if (!$row->store()){
				return true;
			}
		}
		return false;
	}
	
	public function removeUserRoute($routeID,$userID){
		return $this->deleteQuery("DELETE FROM #__sr_userroutes WHERE routeID=".$routeID." AND userID=".$userID);
	}
}

?>